package com.itheima.controller;

import com.itheima.R;
import com.itheima.pojo.SmsHomeAdvertise;
import com.itheima.service.SmsHomeAdvertiseService;
import com.itheima.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 广告crud
 *
 * @author pk
 * @date 2022/10/12/19:06
 */
@RestController
@RequestMapping("home/advertise")
public class SmsHomeAdvertiseController {

    @Autowired
    private SmsHomeAdvertiseService advertiseService;

    /**
     * 添加广告
     *
     * @param advertise
     * @return
     */
    @PostMapping("/create")
    public R saveAdvertise(@RequestBody SmsHomeAdvertise advertise) {

        advertiseService.saveAdvertise(advertise);
        return R.success("添加广告功能执行");

    }

    /**
     * 分页查询广告
     *
     * @param advertise
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public R findAdvertise(String endTime, String name, Integer type,
                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageResult result = advertiseService.findAdvertise(endTime, name, type, pageNum, pageSize);

        return R.success(result);
    }

    /*public static void main(String[] args) {
        System.out.println(new Date("2022/10/10").getTime());
    }*/


    /**
     * 广告删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public R deleteAdvertise(@RequestParam Integer[] ids) {

        advertiseService.deleteAdvertise(ids);

        return R.success("广告删除功能执行");
    }


    /**
     * 修改上下线状态
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/update/status/{id}")
    public R updateStatus(@PathVariable Long id, Integer status) {

        advertiseService.updateStatus(id, status);

        return R.success("广告上下线功能执行");
    }

    /**
     * 修改广告
     *
     * @param id
     * @param advertise
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateAdvertise(@PathVariable Long id, @RequestBody SmsHomeAdvertise advertise) {

        advertiseService.updateAdvertise(id, advertise);

        return R.success("广告修改功能执行");
    }

    /**
     * 根据id查询广告
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R queryAdvertise(@PathVariable Long id) {

        SmsHomeAdvertise advertise = advertiseService.queryAdvertise(id);

        return R.success(advertise);
    }

}
